package com.tf_cut.backend.payment.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.Lists;
import com.tf_cut.backend.common.pojo.PageInfo;
import com.tf_cut.backend.common.pojo.Result;
import com.tf_cut.backend.payment.pojo.ConfigSetting;
import com.tf_cut.backend.payment.service.SellerConfigService;
import com.tf_cut.backend.payment.vo.ConfigSettingVo;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/backend/sellerConfig/")
@Slf4j
public class SellerConfigController {
	
	@Autowired
	private SellerConfigService sellerConfigService;
	
	// 查找类型配置
	@RequestMapping("findListSeller")
	public String findListSeller(String json) {
		ConfigSettingVo configSettingVo = JSON.parseObject(json, ConfigSettingVo.class);
        Result<List<ConfigSetting>> result;
        try {
            PageInfo<ConfigSettingVo> sellerBillAndConsigneeVoPageInfo = sellerConfigService.findListSeller(configSettingVo);
            return JSON.toJSONString(sellerBillAndConsigneeVoPageInfo, SerializerFeature.WriteDateUseDateFormat);
        } catch (Exception e) {
            log.error("querySellerBillList error:{}", e.getMessage(), e);
            result = Result.of(Lists.newArrayList());
            result.setMsg(e.getMessage());
            result.setFlag(false);
        }
        return JSON.toJSONString(result, SerializerFeature.WriteDateUseDateFormat);
	}
	
	@PostMapping("configSave")
	public String configSave(@RequestParam("json")String json){
		ConfigSetting configSetting = JSON.parseObject(json, ConfigSetting.class);
		sellerConfigService.configSave(configSetting);
		return "保存成功";
	}
	
	
	@PostMapping("deleteByName")
	public String deleteByName(@RequestParam("id")String id){
		sellerConfigService.deleteByName(id);
		return "删除成功";
	}
	
	
	@RequestMapping("findByName")
	public String findByName(@RequestParam("id")String id){
		ConfigSetting result = sellerConfigService.findByName(id);
		return JSON.toJSONString(result,SerializerFeature.WriteDateUseDateFormat);
	}

}
